#!/use/bin/python
# coding=utf-8
# 记录UI从1变成0的设备中 DMP命中情况
import datetime
import pymysql
import requests

from dbutils.pooled_db import PooledDB



# proactive_service_conf 数据源
def getConfConnection():
    # 开发环境
    # pool = PooledDB(pymysql, 1, host='172.20.135.96', user='pushdb', passwd='SkYWOrTh$TcOs',
    #                db='proactive_service_conf',
    #                port=3306)  # 1为连接池里的最少连接数
    # 测试环境
    pool = PooledDB(pymysql, 1, host='172.20.150.109', user='test_dmp', passwd='DghHC3lFM1KzT3ZJ',
                db='proactive_service_conf', port=3307)  # 1为连接池里的最少连接数
    # pool = PooledDB(pymysql,1,host='127.0.0.1',user='root',passwd='root',db='life_assistant_data',port=3306) # 5为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur

# proactive_service_data 数据源
def getDataConnection():
    # 开发环境
    #pool = PooledDB(pymysql, 1, host='172.20.151.80', user='pushdb', passwd='SkYWOrTh$TcOs',
    #                db='proactive_service_data',
    #                port=3306)  # 1为连接池里的最少连接数
    # 测试环境
    pool = PooledDB(pymysql, 1, host='172.20.154.103', user='test_dmp', passwd='DghHC3lFM1KzT3ZJ',
                    db='proactive_service_data', port=3407)  # 1为连接池里的最少连接数
    # pool = PooledDB(pymysql,1,host='127.0.0.1',user='root',passwd='root',db='life_assistant_data',port=3306) # 5为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur


def saveDmpLog():
    try:
        conn, cur = getConfConnection()
        selectdeviceSql = """select t.id,t.mac from life_assistant_devices_log t where t.last_update_time is null """
        cur.execute(selectdeviceSql)
        resultNum = cur.fetchall()
        for result in resultNum:
            id = result[0]
            mac = result[1]
            url = f"http://api-hoder.skysrt.com/v2/search/isDevicePolicy?mac={mac}&devInfo={{}}&policyIds=3456,3008&reqSource=17"

            payload = {}
            headers = {}

            response = requests.request("GET", url, headers=headers, data=payload)
            responseText = response.text
            #print(responseText)
            updateSql = f"""
                    update life_assistant_devices_log t set t.bz1= '{responseText}' ,t.last_update_time = now() where t.id = {id}
                """
            if(id is not None ):
                cur.execute(updateSql)
                conn.commit()
            print(f"------------------原始:id:{id},mac:{mac}处理完成 --------------------------")
    except Exception as e:
        print(e)

    finally:
        cur.close()
        conn.close()

def saveCheckDmpLog():
    try:
        conn, cur = getConfConnection()
        selectdeviceSql = """select t.id,t.mac from life_assistant_devices_log t where t.bz2 is null """
        cur.execute(selectdeviceSql)
        resultNum = cur.fetchall()
        for result in resultNum:
            id = result[0]
            mac = result[1]
            url = f"http://api-hoder.skysrt.com/v2/search/isDevicePolicy?mac={mac}&devInfo={{}}&policyIds=3456,3008&reqSource=17"

            payload = {}
            headers = {}

            response = requests.request("GET", url, headers=headers, data=payload)
            responseText = response.text
            #print(responseText)
            updateSql = f"""
                    update life_assistant_devices_log t set t.bz2= '{responseText}' ,t.last_update_time = now() where t.id = {id}
                """
            if(id is not None ):
                cur.execute(updateSql)
                conn.commit()
            print(f"------------------检查:id:{id},mac:{mac}处理完成 --------------------------")
    except Exception as e:
        print(e)

    finally:
        cur.close()
        conn.close()

if __name__ == '__main__':
    saveDmpLog()
    saveCheckDmpLog()